package com.example.androidproject.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.androidproject.R;

import java.io.File;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button mActivityBtnTojumpModle;
    private Uri imageUri;//拍照后剪切用到Uri
    private String picFileFullName;//剪切后图片路径
    private int TAKE_PHOTO = 1;
    private int PHOTO_CUT_RESULT = 2;
    private int IMAGE_PICK_CODE = 3;
    private int CROP_PICTURE = 4;
    private int api = 4;
    private ImageView mActivityIvPhotoModle;
    private Button mActivityBtnTojumpCameraModle;
    private Button mActivityBtnTocameraModle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mActivityBtnTojumpModle = (Button) findViewById(R.id.modle_activity_btn_tojump);
        mActivityBtnTojumpModle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intent, IMAGE_PICK_CODE);
                hihieAlbum();
            }
        });
        mActivityIvPhotoModle = (ImageView) findViewById(R.id.modle_activity_iv_photo);
        mActivityBtnTocameraModle = (Button) findViewById(R.id.modle_activity_btn_tocamera);
        mActivityBtnTocameraModle.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 3) {
                Uri uri = data.getData();
                cropImage(uri, 500, 500, CROP_PICTURE);
                mActivityIvPhotoModle.setImageURI(uri);
            } else if (requestCode == 2) {
                if (data != null) {
                    Parcelable extra = data.getParcelableExtra("data");
                    mActivityIvPhotoModle.setImageBitmap((Bitmap) extra);

                }
            }
        }
    }
    public Bitmap crop(Rect frame, Bitmap image) {
        float[] src = new float[]{frame.right, frame.top};
        Matrix matrix = new Matrix();
        int width = frame.width();
        int height = frame.height();
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        Bitmap originalBitmap = image;
        matrix.postTranslate(-src[0], -src[1]);
        canvas.drawBitmap(originalBitmap, matrix, null);
        return bitmap;
    }

    private void hihieAlbum() {
        File outputImage = new File(Environment.getExternalStorageDirectory(), "output_image.jpg");
        picFileFullName = outputImage.getAbsolutePath();
        try {
            if (outputImage.exists()) {
                outputImage.delete();
            }
            outputImage.createNewFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
        imageUri = Uri.fromFile(outputImage);
    }

    public void cropImage(Uri uri, int outputX, int outputY, int requestCode) {
        //裁剪图片意图
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        //裁剪框的比例，1：1
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        //裁剪后输出图片的尺寸大小
        intent.putExtra("outputX", outputX);
        intent.putExtra("outputY", outputY);
        //图片格式
        intent.putExtra("outputFormat", "JPEG");
        intent.putExtra("noFaceDetection", true);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, requestCode);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.modle_activity_btn_tojump:

                break;
            case R.id.modle_activity_btn_tocamera:
                Intent intent = new Intent(MainActivity.this, CameraActivity.class);
                startActivity(intent);
                break;
            default:

                break;
        }
    }


}